﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Mozlite.WebUI.Mvc4.Backend
{
    /// <summary>
    /// 后台页面基类。
    /// </summary>
    /// <typeparam name="TModel">模型类型。</typeparam>
    public abstract class WebViewPage<TModel> : Mozlite.WebUI.Mvc4.WebViewPage<TModel>
    {
        /// <summary>
        /// 获取当前语言资源字符串。
        /// </summary>
        /// <param name="key">资源键对象。</param>
        /// <returns>返回资源字符串。</returns>
        public override string L(object key)
        {
            var resource = Cores.GetGlobalResourceByKey(key, AreaName);
            if(resource.HasValue())
                return resource;
            return base.L(key);
        }

        /// <summary>
        /// 获取当前语言资源字符串。
        /// </summary>
        /// <param name="key">资源键对象。</param>
        /// <param name="args">格式化参数。</param>
        /// <returns>返回格式化后的资源字符串。</returns>
        public override string L(object key, params object[] args)
        {
            var resource = Cores.GetGlobalResourceByKey(key, AreaName);
            if (resource.HasValue())
                return resource.FormatWith(args);
            return base.L(key, args);
        }

        private string areaName;
        /// <summary>
        /// 当前区域名称。
        /// </summary>
        public string AreaName {
            get {
                if (areaName == null)
                    areaName = ViewContext.GetAreaName();
                return areaName;
            }
        }
    }
}
